package com.me.libgdx_test;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.me.libgdx_test.ClassUnlockablePrototypes.UnlockFactory;

public class ActivityEndScreen implements Screen{
	
	ActivityMain game;
	Stage stage;
	SpriteBatch batch;
	ClassSkinReference skin;
	ClassLogDisplay logger;
	
	public ActivityEndScreen(final ActivityMain game, final ClassScoreHolder score){
		this.game = game;
		logger = new ClassLogDisplay("ActivityIndex");
		logger.print(String.valueOf(Gdx.graphics.getWidth()) + "x" + String.valueOf(Gdx.graphics.getHeight()));
		batch = new SpriteBatch();
		stage = new Stage();
		skin = game.skin;
		//skin = new Skin(Gdx.files.internal("skins/default.json"));
		//skin.getAtlas().getTextures().iterator().next().setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
		Gdx.input.setInputProcessor(stage);
		int width = Gdx.graphics.getWidth();
		int height = Gdx.graphics.getHeight();
		
		Table table = new Table();
		table.setFillParent(true);
		stage.addActor(table);
		
		final Label titleLabel;
		if(score.dungeon_completed()){
			titleLabel = new Label("Success",skin.get_skin());
		}else{
			titleLabel = new Label("Defeat...",skin.get_skin());
		}
		titleLabel.setAlignment(Align.center,Align.center);
		titleLabel.setSize(width/10, height/10);
		titleLabel.setFontScale(9);
		table.add(titleLabel).width(2*width/3).height(height/4).padBottom(height/15);
		table.row();
		
		final Label scoreLabel = new Label(score.get_score_text(),skin.get_skin());
		scoreLabel.setAlignment(Align.center,Align.center);
		scoreLabel.setSize(width/10, height/10);
		scoreLabel.setFontScale(2);
		table.add(scoreLabel).width(2*width/3).height(height/4).padBottom(height/15);
		table.row();
		
		int previous_level = game.unlockable.get_unlockable_prototypes().get_factory().get_current_level();
		int previous_honor = game.unlockable.get_unlockable_prototypes().get_factory().get_player_info("honor");
		
		//Passes the score object to the serializer so that we can parse and send the info to the server later
		game.unlockable.get_unlockable_prototypes().get_factory().add_end_level_info(game.game_vars.player_class, 
				game.game_vars.player_level, score, game.game_vars.raceObjects);
		
		//Reset the activities that reference the unlockables... Selection and GameInfo...
		//MarketActivity is never reset bc the things that are unlocked in the game are 
		//different from the things unlocked in the market
		game.reset_activities();
		
		
		game.unlockable.save_data_to_server();
		
		ClassAchievementEnd acheivement_bar = new ClassAchievementEnd(game,new Point(width,height),
				previous_level,previous_honor,(int)score.ending_gold);
		
		//
		//game.gameInfoScreen.achievement_bar.refresh_achievement_group();
		//game.marketScreen.achievement_bar.refresh_achievement_group();
		
		table.add(acheivement_bar.get_achievement_group()).width(2*width/3).height(height/10).padBottom(10);
		table.row();
		//stage.addActor(acheivement_bar.get_achievement_group());
		
		final TextButton Start_Quest = new TextButton("Main Menu", skin.get_button_skin());
		table.add(Start_Quest).width(2*width/3).height(height/8).pad(height/16);
		table.row();
		Start_Quest.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				game.setScreen(game.indexScreen);
			}
		});
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		stage.draw();
		Table.drawDebug(stage);
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, false);
	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		stage.dispose();
		//skin.get_skin().dispose();
	}

}